package work.xiaohong.mina.handler.plc;

import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.FilterEvent;

/**
 * @author Kevin
 * @data 4/25/2022 3:11 PM
 * @description
 **/
@Slf4j
public class ServerHandler extends IoHandlerAdapter {

  @Override
  public void sessionCreated(IoSession session) throws Exception {
    log.info("服务端连接建立成功！");
    super.sessionCreated(session);
  }

  @Override
  public void sessionOpened(IoSession session) throws Exception {
    log.info("服务端连接打开成功！");
    super.sessionOpened(session);
  }

  @Override
  public void sessionClosed(IoSession session) throws Exception {
    log.info("服务端连接已关闭！");
    super.sessionClosed(session);
  }

  @Override
  public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
    log.info("服务端连接进入空闲状态！关闭连接");
    session.closeNow();
    super.sessionIdle(session, status);
  }

  @Override
  public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
    log.info("服务端连接建立发送错误！");
    super.exceptionCaught(session, cause);
  }

  @Override
  public void messageReceived(IoSession session, Object message) throws Exception {
    log.info("服务端收到消息---->"+message);
    super.messageReceived(session, message);
  }

  @Override
  public void messageSent(IoSession session, Object message) throws Exception {
    log.info("服务端发送消息---->"+message);
    super.messageSent(session, message);
  }

  @Override
  public void inputClosed(IoSession session) throws Exception {
    log.info("服务端流关闭！");
    super.inputClosed(session);
  }

  @Override
  public void event(IoSession session, FilterEvent event) throws Exception {
    log.info("服务端发送过滤事件！");
    super.event(session, event);
  }
}
